package com.daxia.ogmall.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.daxia.ogmall.daobase.GenericDAOHibernate;
import com.daxia.ogmall.dto.DesignProductDTO;
import com.daxia.ogmall.model.Design;
import com.daxia.ogmall.model.DesignProduct;
import com.daxia.ogmall.support.Page;

@Repository
public class DesignProductDAO extends GenericDAOHibernate<DesignProduct> {

    public List<DesignProduct> find(DesignProductDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From DesignProduct n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getDesign() != null) {
        	if (dto.getDesign().getId() != null) {
	            hql.append(" and n.design.id = ? ");
	            paras.add(dto.getDesign().getId());
        	}
        }			
        if (dto.getProduct() != null) {
        	if (dto.getProduct().getId() != null) {
	            hql.append(" and n.product.id = ? ");
	            paras.add(dto.getProduct().getId());
        	}
        	if (dto.getProduct().getBrand() != null) {
        		if (dto.getProduct().getBrand().getId() != null) {
        			hql.append(" and n.product.brand.id = ? ");
        			paras.add(dto.getProduct().getBrand().getId());
        		}
        	}
        }			
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public List<Design> findDistictDesigns(DesignProductDTO dto,Page page) {
    	StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("select distinct n.design From DesignProduct n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getDesign() != null) {
        	if (dto.getDesign().getId() != null) {
	            hql.append(" and n.design.id = ? ");
	            paras.add(dto.getDesign().getId());
        	}
        	if(dto.getDesign().getRecommend() != null) {
        		hql.append(" and n.design.recommend = ? ");
        		paras.add(dto.getDesign().getRecommend());
        	}
        	if(dto.getDesign().getStatus()!=null&&dto.getDesign().getStatus()!=0){
        		hql.append(" and n.design.status = ? ");
        		paras.add(dto.getDesign().getStatus());
        	}
        }			
        if (dto.getProduct() != null) {
        	if (dto.getProduct().getId() != null) {
	            hql.append(" and n.product.id = ? ");
	            paras.add(dto.getProduct().getId());
        	}
        	if (dto.getProduct().getBrand() != null) {
        		if (dto.getProduct().getBrand().getId() != null) {
        			hql.append(" and n.product.brand.id = ? ");
        			paras.add(dto.getProduct().getBrand().getId());
        		}
        		if(dto.getProduct().getBrand().getRecommend()!=null){
        			hql.append(" and n.product.brand.recommend = ?");
        			paras.add(dto.getProduct().getBrand().getRecommend());
        		}
        	}
        }
        hql.append(" order by n.design.id desc ");
        Object paraArr[] = new Object[paras.size()];
        for(int i = 0;i < paras.size();i ++ ){
        	paraArr[i] = paras.get(i); 
        }
        return super.find2(hql.toString(), paraArr, page);
    }
    
    public List<DesignProduct> count(DesignProductDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("select n.*,  count(n.design.id) From DesignProduct n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getDesign() != null) {
        	if (dto.getDesign().getId() != null) {
	            hql.append(" and n.design.id = ? ");
	            paras.add(dto.getDesign().getId());
        	}
        }			
        if (dto.getProduct() != null) {
        	if (dto.getProduct().getId() != null) {
	            hql.append(" and n.product.id = ? ");
	            paras.add(dto.getProduct().getId());
        	}
        	if (dto.getProduct().getBrand() != null) {
        		if (dto.getProduct().getBrand().getId() != null) {
        			hql.append(" and n.product.brand.id = ? ");
        			paras.add(dto.getProduct().getBrand().getId());
        		}
        	}
        }		
        hql.append(" group by n.design.id having size >= 1");
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public DesignProduct findOne(DesignProductDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<DesignProduct> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

	public void deleteByDesignId(Long designId) {
		String hql = "delete from DesignProduct where design.id = ? ";
		super.executeUpdate(hql, new Object[] {designId});
	}
}
